package com.mall.service.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.UmsMenu;
import com.mall.model.domain.UmsRoleMenuRelation;
import com.mall.model.vo.UmsMenuVo;
import com.mall.service.authority.mapper.UmsMenuMapper;
import com.mall.service.authority.service.UmsMenuService;
import com.mall.service.authority.service.UmsRoleMenuRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements UmsMenuService {

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Autowired
    private UmsRoleMenuRelationService umsRoleMenuRelationService;

    @Override
    public List<UmsMenuVo> treeList() {
        //条件构造器
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        //具体条件
        queryWrapper.eq(UmsMenu::getHidden,0);
        //根据具体条件查询UmsMenu
        List<UmsMenu> umsMenuList =umsMenuMapper.selectList(queryWrapper);

        //所有的voList
        List<UmsMenuVo> umsMenuVoList = new ArrayList<>();

        //遍历基本umsMenu
        for (UmsMenu umsMenu : umsMenuList) {
            if (umsMenu.getParentId()==0){
                //新建Vo对象
                UmsMenuVo umsMenuVoTemp = new UmsMenuVo();
                //把umsMenu的属性复制到vo对象
                BeanUtils.copyProperties(umsMenu,umsMenuVoTemp);
                //把vo对象放入vo的list
                umsMenuVoList.add(umsMenuVoTemp);
            }
        }
        return findChildren(umsMenuList, umsMenuVoList);
    }

    //获取角色相关菜单
    public List<UmsMenu> listMenu(Integer roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId,roleId);
        List<UmsRoleMenuRelation> list = umsRoleMenuRelationService.list(queryWrapper);

        List<Long> ids = list.stream().map(item -> {
            return item.getMenuId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UmsMenu::getId,ids);
        List<UmsMenu> menuList = umsMenuMapper.selectList(wrapper);
        return menuList;
    }





    /**
     *
     * @param umsMenuList
     * @param umsMenuVoList
     * @return
     */
    private List<UmsMenuVo> findChildren(List<UmsMenu> umsMenuList, List<UmsMenuVo> umsMenuVoList){

        //遍历需要查找的父级菜单
        for (UmsMenuVo umsMenuVo : umsMenuVoList) {
            //创建子菜单list
            List<UmsMenuVo> umsMenuVoListTemp = new ArrayList<>();
            //循环所有菜单
            for (UmsMenu umsMenu : umsMenuList) {
                //将所有菜单
                if (umsMenu.getParentId().equals(umsMenuVo.getId())){
                    UmsMenuVo umsMenuVoTemp = new UmsMenuVo();
                    BeanUtils.copyProperties(umsMenu,umsMenuVoTemp);
                    umsMenuVoListTemp.add(umsMenuVoTemp);
                }
            }
            //再查一下子菜单
            findChildren(umsMenuList,umsMenuVoListTemp);
            //将整理完成的子菜单存入Children
            umsMenuVo.setChildren(umsMenuVoListTemp);
        }
        return umsMenuVoList;
    }
}
